﻿using EMS.system.Common;
using EMS.system.Entityies.MasterData.CustomerManagementModel;
using EMS.system.Entityies.WarehouseManagement.SalesOutModel;
using EMS.system.IEntityAppService.MasterData.CustomerManagementIService.CustomerManagementDTO;
using EMS.system.IEntityAppService.MasterData.SalesOutService;
using EMS.system.IEntityAppService.MasterData.SalesOutService.SalesOutDTOs;
using ServiceStack.Script;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace EMS.system.EntityAppService.MasterData.SalesOutService
{
    public class SalesOutAppService:ApplicationService,ISalesOutAppService
    {
        /// <summary>
        /// 销售出库仓储
        /// </summary>
        private readonly IRepository<SalesOut, int> _salesOutrepository;
        /// <summary>
        /// 客户管理仓储
        /// </summary>
        private readonly IRepository<CustomerManagement, int> _customermanagementrepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="salesOutrepository"></param>
        /// <param name="customermanagementrepository"></param>
        public SalesOutAppService(IRepository<SalesOut, int> salesOutrepository, IRepository<CustomerManagement, int> customermanagementrepository)
        {
            _salesOutrepository = salesOutrepository;
            _customermanagementrepository = customermanagementrepository;
        }
        /// <summary>
        /// 获取销售出库显示 分页 查询
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="outCode"></param>
        /// <returns></returns>
        public async Task<PagedResult> GetSalesOutList(int pageIndex,int pageSize,string? outCode)
        {
            var predicate = PredicateBuilder.New<SalesOut>(true);
            if (!string.IsNullOrEmpty(outCode))
            {
                predicate = predicate.And(m => m.OutCode.Contains(outCode));
            }
            var salesOut = from a in (await _salesOutrepository.GetListAsync(predicate))
                           join b in _customermanagementrepository.GetListAsync().Result on a.CustomerID equals b.CustomerCode
                           select new SalesOutDTO
                           {
                               Id = a.Id,
                               OutCode=a.OutCode,
                               OutName=a.OutName,
                               OutDate=a.OutDate,
                               SalesOrderID=a.SalesOrderID,
                               CustomerID=a.CustomerID,
                               Dstatus=a.Dstatus,
                               CustomerName=b.CustomerName

                           };
            PagedResult result = new PagedResult();
            result.PageCount = (int)Math.Ceiling((double)(salesOut.Count()==0?1:salesOut.Count())/pageSize);
            result.PageSize = pageSize;
            result.CurrentPage = pageIndex;
            result.RowCount=salesOut.Count();
            result.Queryable = salesOut.Skip((pageIndex - 1) * pageSize).Take(pageSize).AsQueryable();
            return result;

        }
        /// <summary>
        /// 获取客户管理列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<CustomerManagementInsertDTO>> GetCustomerManagement()
        {
            var customerManagement = await _customermanagementrepository.GetListAsync();
            var customerManagements = customerManagement.MapToList<CustomerManagementInsertDTO>();
            return customerManagements;
        }
        /// <summary>
        /// 添加销售出库
        /// </summary>
        /// <param name="salesOutDTO"></param>
        /// <returns></returns>
        public async Task<int> CreateSalesOut(SalesOutDTO salesOutDTO)
        {
            var outName = _salesOutrepository.CountAsync(m => m.OutName == salesOutDTO.OutName).Result;
            if (outName > 0)
            {
                return -1;
            }
            var salesOrderID = _salesOutrepository.CountAsync(m => m.SalesOrderID == salesOutDTO.SalesOrderID).Result;
            if (salesOrderID > 0)
            {
                return -2;
            }
            
            var salesOut = salesOutDTO.MapTo<SalesOut>();
            await _salesOutrepository.InsertAsync(salesOut);
            return 1;
        }
        /// <summary>
        /// 删除销售出库
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<int> DeleteSalesOut(string ids)
        {
            string[] array= ids.Split(',');
            await _salesOutrepository.DeleteAsync(m => ids.Contains(m.Id.ToString()));
            return 1;
        }
        /// <summary>
        /// 获取销售出库单主键id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<SalesOutDTO> GetSalesOutById(int id)
        {
            var salesOut=await _salesOutrepository.GetAsync(id);
            var salesOutDTO=salesOut.MapTo<SalesOutDTO>();
            return salesOutDTO;
        }
        /// <summary>
        /// 更新销售出库
        /// </summary>
        /// <param name="salesOutDTO"></param>
        /// <returns></returns>
        public async Task<int> UpdaetSalesOut(SalesOutDTO salesOutDTO)
        {
            var outCode = _salesOutrepository.CountAsync(m => m.OutCode == salesOutDTO.OutCode).Result;
            if (outCode > 0)
            {
                return -1;
            }
            var outName = _salesOutrepository.CountAsync(m => m.OutName == salesOutDTO.OutName).Result;
            if (outName > 0)
            {
                return -2;
            }
          
            var salesOut= salesOutDTO.MapTo<SalesOut>();
            var salesOuts = await _salesOutrepository.UpdateAsync(salesOut);
            return 1;
        }
    }
}
